% Replication of section 3, main results in Table 4
% "UI Generosity and Job Acceptance:Effects of the 2020 CARES Act"
% by N. Petrosky-Nadeau and R.G. Valletta
% JPE Macro, forthcoming
% Date: May 21, 2024

clear all; close all; clc;
Figure_settings

%% Import data moments
opts = spreadsheetImportOptions("NumVariables", 6);

% Specify sheet and range
opts.Sheet = "Revised Occupations";
opts.DataRange = "B5:G19";

% Specify column names and types
opts.VariableNames = ["fm", "sm", "fm_1", "sm_1", "mu_w", "sig_w"];
opts.VariableTypes = ["double", "double", "double", "double", "double", "double"];

% Import the data
InGroupInputs = readtable("ReservationBenefit_In_Group_Inputs.xlsx", opts, "UseExcel", false);
InGroupInputs = table2array(InGroupInputs);
clear opts
InGroupInputs_2010_case     = [InGroupInputs(:,1:2) InGroupInputs(:,5:6)];



%% set common parameter values:
beta    = exp(-5/1200);     % discount factor, 5% annually
r       = 1/beta - 1   ;     % discount rate
tau     = 0.5;              % regular replacement rate
Tc      = 39;               % full duration of UI benefits, T weeks
Tp      = 17;
params  = {tau, r, Tc, Tp};

% Groups_j
Groups  = {'Overall', 'Age_25_to_54', 'Less_than_HS', 'High_school','College_and_above'...
    'Management','Prof_Techn','Services','Sales','Admin','Farm_Fish',...
    'Constr_Extractr','Inst_Maint_Rep','Prod','Transp_Materials'};

 
% Inputs from group specific calculations:
%Inputs_j    = {fm, sm, log_wage_mean, log_wage_std};
j=1;
for j=1:max(size(Groups))
    Group_j = Groups{j}
    Inputs_j    = InGroupInputs_2010_case(j,:);
    [Results_j] = Group_j_ResBenef(Inputs_j, params,Group_j);
    Results_js(:,j) = Results_j;
    assignin('base',Group_j, Results_j);
end


%% Summary Table
Stat    = {'Earnings, median'; 'Base UI, median';'CARES UI, median';...
    'Res Benef CARES, 8 weeks'; ...
    'Share Rej Offer 4 weeks, CARES';'Share Rej Offer 8 weeks, CARES';...
    'Share Rej Offer 12 weeks, CARES';  'Share Rej Offer, CARES Average 4-12 weeks pooled'};

T   = table(Stat,Overall, Age_25_to_54, Less_than_HS, High_school,College_and_above,...
    Management,Prof_Techn,Services,Sales,Admin,Farm_Fish,...
    Constr_Extractr,Inst_Maint_Rep,Prod,Transp_Materials)



%% FIGURE - RES BENEF BY OCCUPATION, 8 weeks out
X_occup2 = categorical({'Management','Prof_Techn','Services','Sales','Admin','Farm_Fish',...
    'Constr_Extractr','Inst_Maint_Rep','Prod','Transp_Materials'});
X_occup2 = reordercats(X_occup2,{'Management','Prof_Techn','Services','Sales','Admin','Farm_Fish',...
    'Constr_Extractr','Inst_Maint_Rep','Prod','Transp_Materials'});

Fig_Occupations_Medians_Revised





%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Reservation Benefits function
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [Results_j] = Group_j_ResBenef(Inputs_j,params,Group_j)

fm   = Inputs_j(1,1); 
sm   = Inputs_j(1,2);
log_wage_mean   = Inputs_j(1,3); 
log_wage_std   = Inputs_j(1,4); 

% Remaining inputs
fw          = 1-(1-fm)^0.25;        % weekly job finding rate
sw_fn =  @(sw)(sm - sw*((1-fw)*(fw*sw + (1-fw)^2) + fw*(sw*(1-fw) + (1-sw)*sw))  ...
        - (1-sw)*(sw*(fw*sw + (1-fw)^2)     + (1-sw)*(sw*(1-fw) + (1-sw)*sw) ));
sw          = fsolve(sw_fn,0.1);    % weekly job separation rate

% Uncommnent to print durations of unemployment (weeks) and employment
% (years) spells report in columns (2) and (3) of Table 4.
U_dur = 1/fw
E_dur = 1/(sw*52)


% Use a truncated log-normal 
pd = makedist('Log Normal','mu',log_wage_mean,'sigma',log_wage_std);
draws = 5000000;
Wage_dist = random(pd,draws,1);



Inputs = {Wage_dist, fw, sw};

% Compute reservation benefits within each group i
Setting = 2;      % =2 job loss in first week of CARES act
[Output_Group]  = ResBenef_Hetero_W_func(Inputs,params,Setting);
[br_t, b, bc]   = Output_Group{:};

ResBenefs  = br_t;
BaseBenefs = b;
CARESBenefs= bc;
CARES_ReplRate = bc./Wage_dist;

RejctOfferShare_base     = sum([(ResBenefs(:,8)-BaseBenefs)]<0)/numel(Wage_dist);
RejectOfferShare_CARES4   = sum([(ResBenefs(:,4)-CARESBenefs)]<0)/numel(Wage_dist);
RejectOfferShare_CARES8   = sum([(ResBenefs(:,8)-CARESBenefs)]<0)/numel(Wage_dist);
RejectOfferShare_CARES12 = sum([(ResBenefs(:,12)-CARESBenefs)]<0)/numel(Wage_dist);

ResBenefs_4_12_pool     = reshape(ResBenefs(:,4:12), [9*draws 1]);
CARESBenefs_pool        = [CARESBenefs;CARESBenefs;CARESBenefs;CARESBenefs;CARESBenefs;CARESBenefs;CARESBenefs;CARESBenefs;CARESBenefs];

RejectOfferShare_CARES_Average_4_12_pool = sum([(ResBenefs_4_12_pool-CARESBenefs_pool)]<0) /(numel(Wage_dist)*9); 


% medians for reporting:
med_Earnings = median(Wage_dist);
med_base_UI  = median(BaseBenefs);
med_CARES_UI  = median(CARESBenefs);
med_CARES_ReplRate = median(CARES_ReplRate);
med_Res_Benef_CARES12   = median(ResBenefs(:,12));
med_Res_Benef_CARES8  = median(ResBenefs(:,8));
med_Res_Benef_CARES4  = median(ResBenefs(:,4));


Results_j = [med_Earnings;med_base_UI;med_CARES_UI;...
     med_Res_Benef_CARES8; ...
    RejectOfferShare_CARES4; RejectOfferShare_CARES8; RejectOfferShare_CARES12;...
    RejectOfferShare_CARES_Average_4_12_pool];

end




